import config from './config.js'
// 文件上传
const upload = (path, formData) => {
    // const baseUrl = uni.getStorageSync("baseUrl") ? uni.getStorageSync("baseUrl") : 'http://220.171.59.122:8083/'
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            // url: baseUrl+'core/attach/uploadAttach',
            url: config.baseUrl+'core/attach/uploadAttach',
            filePath: path,
            name: 'file',
            header:{
                Authorization: uni.getStorageSync("Authorization")
            },
            formData: formData,
            success: (res) => {
                // console.log('上传成功',res)
                if (res.statusCode == 200 ){
                    resolve(JSON.parse(res.data))
                }else{
                    uni.$u.toast(res.errMsg)
                }
            },
            fail: (err) => {
                uni.$u.toast(err.errMsg)
                reject(err)
            },
            complete: (res) => {
                uni.hideLoading()
                // console.log('上传完成',res)
            }
        });
    });
}


const uploadFiles = (files, formData) => {
    formData.Authorization = uni.getStorageSync("Authorization")
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: 'http://220.171.59.122:8083/core/attach/uploadAttach',
            files: files,
            formData: formData,
            success: (res) => {
                if (res.statusCode == 200) {
                    resolve(JSON.parse(res.data))
                } else {
                    uni.$u.toast(res.errMsg)
                }
            },
            fail: (err) => {
                uni.hideLoading()
                uni.$u.toast(err.errMsg)
                reject(err)
            },
            complete: (res) => {
                // console.log('上传完成',res)
            }
        });
    });
}

module.exports = {
    upload,
    uploadFiles
}


